package com.ansion.basedb.dao.entity;

import androidx.annotation.NonNull;


import com.ansion.basedb.database.SqlDBInterface;
import com.ansion.basedb.database.sql.ColumnType;
import com.ansion.basedb.database.sql.SQLField;
import com.ansion.basedb.database.sql.SQLRow;
import com.ansion.basedb.database.sql.SQLTable;
import com.ansion.constant.BConstant;
import com.ansion.utils.ArrayUtil;

import java.util.Objects;

/**
 * created by zhaoyuntao
 * on 07/12/2023
 */
public class GroupUserEntity implements SqlDBInterface {
    private String gid;
    private String memberUid;
    private FtsUserEntity userBeanForUI;

    @Override
    public SQLTable getTable() {
        return new SQLTable(UserTables.GROUP_USER.TABLE_NAME, BConstant.MIGRATE.VERSION_CODE_DB).mutPrimaryKey(true)//手动指定采用联合主键
                .addField(new SQLField(UserTables.GROUP_USER.KEY_GID).setType(ColumnType.TYPE_TEXT).setPrimaryKey(true))
                .addField(new SQLField(UserTables.GROUP_USER.KEY_UID).setType(ColumnType.TYPE_TEXT).setPrimaryKey(true).index(true));
    }

    @Override
    public @NonNull SQLRow writeToSQLRow() {
        return new SQLRow().put(UserTables.GROUP_USER.KEY_GID, gid)
                .put(UserTables.GROUP_USER.KEY_UID, memberUid);
    }

    @Override
    public void readFromSQLRow(@NonNull SQLRow row) {
        memberUid = row.getAsString(UserTables.GROUP_USER.KEY_UID);
        gid = row.getAsString(UserTables.GROUP_USER.KEY_GID);
    }


    public GroupUserEntity setGid(String gid) {
        this.gid = gid;
        return this;
    }

    public GroupUserEntity setMemberUid(String memberUid) {
        this.memberUid = memberUid;
        return this;
    }

    public String getGid() {
        return gid;
    }

    public String getMemberUid() {
        return memberUid;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GroupUserEntity that = (GroupUserEntity) o;
        return Objects.equals(gid, that.gid) && Objects.equals(memberUid, that.memberUid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(gid, memberUid);
    }

    public void setUserBeanForUI(FtsUserEntity userBeanForUI) {
        this.userBeanForUI = userBeanForUI;
    }

    public FtsUserEntity getUserBeanForUI() {
        return userBeanForUI;
    }

    @NonNull
    @Override
    public String toString() {
        return ArrayUtil.combine(gid, memberUid);
    }
}
